import type { ApiResponse, Categories, IndexArticles, Post, SiteConfigData } from '~/stores/type';

export const base_url = 'https://www.dzklawyer.com';
export const site_config_uri = '/api.php/portal/site_config';
export const categories_uri = '/api.php/portal/categories';
export const index_articles_uri = '/api.php/portal/index_articles';
export const articles_uri = '/api.php/portal/articles/getArticleList?relation=categories';
export const article_info = '/api.php/portal/read_articles/';
export const recommend_post = '/api.php/portal/articles/recommendArticles';
export const last_next_post = '/api.php/portal/articles/getLastNextArticle';
export const latest_post_uri = '/api.php/portal/articles/latestArticles';
export const hottest_post_uri = '/api.php/portal/articles/hottestArticles';
export const about_url = '/api.php/portal/pages/57';
const options: any = {
  headers: { 'XX-Api-Version': '1.0.0' },
};
async function requestServer<T>(url: string, params?: object) {
  if (params) {
    options['params'] = params;
  }
  return await useFetch<ApiResponse<T>>(url, options);
}
async function requestClient<T>(url: string, params?: object) {
  if (params) {
    options['params'] = params;
  }
  return await $fetch<ApiResponse<T>>(url, options);
}
export async function get_site_config() {
  const res_data = await requestClient<SiteConfigData>(base_url + site_config_uri);
  const data = res_data.data;
  return data;
}

export async function get_categories() {
  const res_data = await requestClient<Categories[]>(base_url + categories_uri);
  const data = res_data.data;
  return data;
}

export async function get_index_articles() {
  const res_data = await requestClient<IndexArticles[]>(base_url + index_articles_uri);
  const data = res_data.data;
  return data;
}

export async function get_post_by_category(category_id: number, page: number = 1) {
  const params = {
    category_id,
    page,
  };
  const res_data = await requestClient<PostPage>(base_url + articles_uri, params);
  const data = res_data.data;
  return data;
}
export async function get_post_by_category_client(category_id: number, page: number = 1) {
  const params = {
    category_id,
    page,
  };
  const res_data = await requestClient<PostPage>(base_url + articles_uri, params);
  const data = res_data.data;
  return data;
}

export async function get_post_info(post_id: number) {
  const res_data = await requestClient<Post>(base_url + article_info + post_id);
  const data = res_data.data;
  return data;
}

export async function get_post_relation(post_id: number) {
  const res_data = await requestClient<Post[]>(base_url + recommend_post + '?id=' + post_id);
  const data = res_data.data;
  return data;
}

export async function get_last_next_post(post_id: number) {
  const res_data = await requestClient<TopDownPost>(base_url + last_next_post + '?id=' + post_id);
  const data = res_data.data;
  return data;
}

export async function get_latest_post(post_id: number) {
  const res_data = await requestClient<Post[]>(base_url + latest_post_uri + '?id=' + post_id);
  const data = res_data.data;
  return data;
}
export async function get_hottest_post(post_id: number) {
  const res_data = await requestClient<Post[]>(base_url + hottest_post_uri + '?id=' + post_id);
  const data = res_data.data;
  return data;
}

export async function get_about_page() {
  const res_data = await requestClient<Post>(base_url + about_url);
  const data = res_data.data;
  return data;
}
